﻿using System;
using System.Collections.Generic;
using System.Data;

namespace Personalplaner.Modules.Dienstplan.App
{
	public class PlanAutomation
	{
		/// <summary>
		/// key=dienstid, value=count per day
		/// </summary>
		static Dictionary<int, int> dienste;

		/// <summary>
		/// value=count per month
		/// </summary>
		static List<int> personen = new List<int>();

		static List<int> abteilungen = new List<int>();
		//static int planid;
		//static int schemaid;

		public static void ScedulePlan(int planid)
		{
			int schemaid = Gencode.PrsCommon.Data.Plan.PlanData.GetPlanSchemaID(planid);
			DataView dv = Gencode.PrsCommon.Data.Plan.PlanData.GetAbteilungenInSchema(schemaid);
			foreach (DataRowView row in dv)
				abteilungen.Add(int.Parse(row["abteilungid"].ToString()));

			dv.Dispose();

			string date = Gencode.PrsCommon.App.DateUtil.ConvertSqlToDate(Gencode.PrsCommon.Data.Plan.PlanData.GetPlanDate(planid)[0].Row["bis"].ToString());
			int daycount = int.Parse(Gencode.PrsCommon.App.DateUtil.GetDay(date));
			int month = int.Parse(Gencode.PrsCommon.App.DateUtil.GetMonth(date));
			int year = int.Parse(Gencode.PrsCommon.App.DateUtil.GetYear(date));
			Random random = new Random();

			foreach (int abteilungid in abteilungen)
			{
				//create list with dienste to
				InitializeDienste(abteilungid, schemaid);

				//calculate progress
				int progresTotal = 0;
				int progres = 0;
				foreach (int cnt in dienste.Values)
				{
					progresTotal += cnt;
				}
				progresTotal *= daycount;

				//loop for every dienst
				foreach (int dienstid in dienste.Keys)
				{
					//loop dienst for every day in plan
					for (int i = 1; i <= daycount; i++)
					{
						date = Gencode.PrsCommon.App.DateUtil.DateBuilder(i, month, year);

						int countInPlan = Gencode.PrsCommon.Data.Plan.PlanData.GetDienstCountInPlanPerDay(dienstid, planid, abteilungid, date);

						//create list with people
						if (countInPlan < dienste[dienstid])
						{
							InitializePersonen(abteilungid, planid, date);
						
							if (personen.Count > 0)
							{
								//loop dienstcount for day
								while (countInPlan < dienste[dienstid])
								{
									//go down list of people and start adding dienste (random)
									//int rnd = random.Next(0, personen.Count - 1);
									int rnd = random.Next(0, personen.Count);

									//check if people are free
									if (personen.Count > 0)
									{
										if (Gencode.PrsCommon.Data.Plan.PlanData.GetDienstCountInPlanPerDay(planid, abteilungid, date, personen[rnd]) < 1)
										{
											//add dienst to person
											Gencode.PrsCommon.Data.Dienst.DienstData.SaveDienstInPlan(planid, personen[rnd], abteilungid, date, dienstid);
											//check how many are already planed
											countInPlan++;// = Gencode.PrsCommon.Data.Plan.PlanData.GetDienstCountInPlanPerDay(dienstid, planid, abteilungid, date);

											//create list with people
											personen.RemoveAt(rnd);
											//InitializePersonen(abteilungid, planid, date);

											progres++;
											SetProgress(progres, progresTotal);
										}
									}
									else
									{
										//no more people
										countInPlan = dienste[dienstid];
									}
								}
							}
						}
					}
				}
				progres = 0;
			}
			SetProgress(0, 100);
		}

		private static void InitializeDienste(int abteilungid, int schemaid)
		{
			dienste = new Dictionary<int, int>();
			DataView dv = Gencode.PrsCommon.Data.Dienst.DienstData.GetDienstSchema(schemaid, abteilungid, Verwaltung.Mandant);
			foreach (DataRowView row in dv)
			{
				dienste.Add(int.Parse(row["dienstid"].ToString()),
					int.Parse(row["einsatzprotag"].ToString()));
			}
			dv.Dispose();
		}

		private static void InitializePersonen(int abteilungid, int planid, string date)
		{
			//DataTable dv = Gencode.PrsCommon.Data.Plan.PlanData.GetPersonenWithoutDienst(planid, abteilungid, date);
			//personen.Clear();
			//for (int i = 0; i < dv.Rows.Count; i++)
			//    personen.Add(int.Parse(dv.Rows[i]["prsid"].ToString())); //[ [i].Row["prsid"].ToString()));
			
			//dv.Dispose();

			DataView dv = Gencode.PrsCommon.Data.Plan.PlanData.GetPersonenWithoutDienstDv(planid, abteilungid, date);
			personen.Clear();
			for (int i = 0; i < dv.Count; i++)
				personen.Add(int.Parse(dv[i]["prsid"].ToString()));

			dv.Dispose();
		}

		private static void SetProgress(int progres, int total)
		{
			Personalplaner.App.Util.SetProgress((100 / total) * progres);
		}
	}
}
